﻿using System.Reflection;
using Autofac;
namespace CrazyTalk.DALFactory
{
    /// <summary>
    /// 获取数据访问对象
    /// 使用了autofac进行封装映射
    /// </summary>
    public sealed class DataAccess
    {
        private static IContainer _container;
        private static readonly object SynLock = new object();
        public static T Get<T>()
        {
            lock (SynLock)
            {
                if (_container == null)
                {
                    CreateHostContainer();
                }
            }
            return _container.Resolve<T>();
        }
        public static void CreateHostContainer()
        {
            var builder = new ContainerBuilder();
            builder.RegisterAssemblyTypes(Assembly.Load("CrazyTalk.MSSQLDAL"), Assembly.Load("CrazyTalk.IDAL")).AsImplementedInterfaces();
            //builder.RegisterType<UserInfoDAL>().As<IUserInfoDAL>();
            var container = builder.Build();
            _container = container;
        }
    }
}